package com.qzdsoft.erpcloud.controller.com;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qzdsoft.erpcloud.controller.BaseController;
import com.qzdsoft.erpcloud.service.act.CenterCompanyService;
import com.qzdsoft.erpcloud.service.com.DepartmentService;
import com.qzdsoft.erpcloud.service.com.StaffService;
import com.qzdsoft.erpcloud.service.sys.Permission;
import com.qzdsoft.erpcloud.service.sys.SysLog;
import com.qzdsoft.erpcloud.service.sys.SysRoleService;
import com.qzdsoft.erpcloud.service.sys.SysUserService;
import com.qzdsoft.erpcloud.vo.com.StaffInfo;
import com.qzdsoft.erpcloud.vo.com.StaffQueryParam;
import com.qzdsoft.utils.ExceptionUtil;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.ResultEnum;

/**
 * 员工管理， yyb
 */
@Controller
@RequestMapping("/com/staff")
public class StaffController extends BaseController{
	 private static final Logger logger = LoggerFactory.getLogger(StaffController.class);
	private static final String  BASEPATH = "com/staff";
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private StaffService staffService;
    @Autowired
    private CenterCompanyService actCenterCompanyService;
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private DepartmentService departmentService;
//    @Autowired
//    private RedisHelper redisHelper;
    @Permission(validate=false)
    @RequestMapping(value = "index", method = RequestMethod.GET)
    public String index(HttpServletRequest request, Model model) {
//        model.addAttribute("actCenterCompanyMap", redisHelper.getActCenterCompanyMap());
    	model.addAttribute("roleList", sysRoleService.getAllRoleList());
    	//有权限的公司
    	model.addAttribute("actCenterCompanyMap", getUser().getCompanys());
        return BASEPATH+"/index";
    }

    /**
     * 查询所有的员工信息
     * @param request
     * @param response
     * @return
     * @throws Exception 
     */
  
    @ResponseBody
    @RequestMapping("/data")
    public LayTableResponse data(HttpServletRequest request, StaffQueryParam param) {
    	logger.debug("员工信息管理初始化查询companyId={},name={}",param.getCompanyId(),param.getName());
        if(param.isFirst()) {
        	return LayTableResponse.nullDataPage();
        }else{
        	return staffService.selectData(param);
        }
    }
 
    /**
     * 员工信息管理修改页面数据
     * @param request
     * @param id
     * @param model
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/edit")
    public ModelAndView edit( @RequestParam("id") Integer id) {
    	ModelAndView mv =new ModelAndView();
    	mv.addObject("roleList", sysRoleService.getAllRoleList());
    	mv.addObject("actCenterCompanyMap", actCenterCompanyService.getAllCompany());
        StaffInfo staff = staffService.getById(id);
        mv.addObject("dept", departmentService.getByCompanyId(staff.getCompanyId()));
        mv.addObject("staff", staff);
        mv.setViewName(BASEPATH+"/edit");
        return mv;
    }
    @Permission(validate=false)
    @RequestMapping("/addinit")
    public ModelAndView toAdd(@RequestParam("companyId") Integer companyId) {
    	ModelAndView mv =new ModelAndView();
    	mv.addObject("roleList", sysRoleService.getAllRoleList());
    	mv.addObject("actCenterCompanyMap", actCenterCompanyService.getAllCompany());
//        mv.addObject("dept", departmentService.getByCompanyId(companyId));
        mv.setViewName(BASEPATH+"/add");
        return mv;
    }
    /**
     * 详情
     * @param id
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/view")
    public ModelAndView view(@RequestParam("id") Integer id) {
    	ModelAndView mv =new ModelAndView();
    	StaffInfo staff = staffService.getById(id);
    	mv.addObject("staff", staff);
        mv.setViewName(BASEPATH+"/view");
        return mv;
    }
    
    /**
     * 新增保存
     * @param request
     * @param staff
     * @return
     */
    @SysLog(description="员工管理新增保存")
    @ResponseBody
    @RequestMapping("/add")
    public Response add(HttpServletRequest request, StaffInfo staff) {
//        staff.setCompany_id(SessionUtil.getLoggedCompanyId(request, staff.getCompany_id()));
    	try{
    		 return staffService.edit(staff);
    	}catch(Exception e) {
    		e.printStackTrace();
    		logger.error("新增保存员工信息失败，错误信息:",ExceptionUtil.getStackTrace(e));
    	}
       return new Response(ResultEnum.ERROR);
    }
    
    /**
     * 修改保存
     * @param request
     * @param staff
     * @return
     */
    @SysLog(description="员工管理修改保存")
    @ResponseBody
    @RequestMapping("/edit_execute")
    public Response editExecute(HttpServletRequest request, StaffInfo staff) {
//        staff.setCompany_id(SessionUtil.getLoggedCompanyId(request, staff.getCompany_id()));
    	try{
    		 return staffService.edit(staff);
    	}catch(Exception e) {
    		e.printStackTrace();
    		logger.error("修改保存员工信息失败，错误信息:",ExceptionUtil.getStackTrace(e));
    	}
       return new Response(ResultEnum.ERROR);
    }
    
    @SysLog(description="员工管理删除")
    @RequestMapping("/del_execute")
    @ResponseBody
    public Response deleteStaff(Integer id) {
    	logger.debug("删除员工信息,id={}",id);
    	try{
    		return staffService.deleteStaff(id);
    	}catch(Exception e){
    		e.printStackTrace();
    		logger.error("删除员工信息失败，错误信息:",ExceptionUtil.getStackTrace(e));
    	}
    	return new Response(ResultEnum.ERROR);
    }
    
    
}
